import * as THREE from 'three';

class QuadraticCurve {
    constructor(a, b, c) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.points = [];
        this.line = null;
    }

    // Calculate points for the quadratic curve
    calculatePoints(xMin = -10, xMax = 10, steps = 100) {
        this.points = [];
        const stepSize = (xMax - xMin) / steps;

        for (let x = xMin; x <= xMax; x += stepSize) {
            const y = this.a * x * x + this.b * x + this.c;
            this.points.push(new THREE.Vector3(x, y, 0));
        }
    }

    // Create and return the curve line
    createLine(color = 0x0000ff, lineWidth = 1) {
        if (this.points.length === 0) {
            this.calculatePoints();
        }

        const geometry = new THREE.BufferGeometry().setFromPoints(this.points);
        const material = new THREE.LineBasicMaterial({
            color: color,
            linewidth: lineWidth
        });

        this.line = new THREE.Line(geometry, material);
        return this.line;
    }

    // Update curve coefficients and redraw
    updateCoefficients(a, b, c) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.calculatePoints();
        
        if (this.line) {
            const geometry = new THREE.BufferGeometry().setFromPoints(this.points);
            this.line.geometry.dispose();
            this.line.geometry = geometry;
        }
    }
}

export default QuadraticCurve;